"""
    数字迭代及yield生成器

"""


class Number:
    """可迭代对象"""

    def __init__(self, end_value):
        self.end_value = end_value

    def __iter__(self):
        return NumberIterator(self.end_value)


class NumberIterator:
    """迭代器"""

    def __init__(self, num):
        self.num = num
        self.index = 0

    def __next__(self):
        if self.index > self.num:
            raise StopIteration
        so = self.index
        self.index += 1
        return so


ls = Number(3)
for i in ls:
    print(i)

print("------------------------")


# 利用yield, 同样可以实现迭代方法


class Numbers:
    def __init__(self, num):
        self.num = num

    def __iter__(self):
        number = 0
        while number < self.num:
            yield number
            number += 1


so = Numbers(5)
for c in so:
    print(c)
